package mysql

import (
	"gorm.io/gorm"
	"strings"
)

type House struct {
	gorm.Model
	HouseSize        float64
	Text             string
	HouseLocal       string
	AddressId        int
	Price            float64
	CharacteristicId int
	Status           int
}

type Address struct {
	gorm.Model
	Province string
	City     string
	district string
}

type Characteristic struct {
	gorm.Model
	Text string
}

func Create(house House) House {
	h := House{}
	db.Where("text", house.Text).Find(&h)
	if h.ID != 0 {
		return House{}
	}
	db.Create(&house)
	return house
}

func HouseList(page int, search string) []House {
	sts := strings.TrimSpace(search)
	pageSize := 3
	h := []House{}
	if len(sts) == 0 {
		if page == 0 {
			page = 1
		}
		offset := (page - 1) * pageSize
		db.Offset(offset).Limit(pageSize).Find(&h)
		return h
	}
	if page == 0 {
		page = 1
	}
	offset := (page - 1) * pageSize
	db.Where("text", search).Offset(offset).Limit(pageSize).Find(&h)
	return h
}
